package com.example.liang.basamovie.adapter;

import android.app.Instrumentation;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liang.basamovie.R;
import com.example.liang.basamovie.bean.FindComment;
import com.example.liang.basamovie.bean.FindPlayerInfoBean;
import com.example.liang.basamovie.utils.ImageLoaderCircleUtils;
import com.example.liang.basamovie.utils.ImageLoaderUtils;

import java.util.List;

/**
 * Created by hudongwx on 17-2-17.
 */
public class FindPlaerInfoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    FindPlayerInfoBean playerInfoBean;
    Context context;
    List<FindComment> list;

    public FindPlaerInfoAdapter(FindPlayerInfoBean playerInfoBean, Context context) {
        this.playerInfoBean = playerInfoBean;
        this.context = context;
        setList();
    }

    private void setList() {
        list=playerInfoBean.getList();
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0){
            return R.layout.fragment_find_plaerinfo_layout;
        }else {
            return R.layout.fragment_playerinfoitem_layout;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(viewType, parent, false);
        if(viewType==R.layout.fragment_find_plaerinfo_layout){
            return new MyViewHolder1(view);
        }else {
            return new MyViewHolder2(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(position==0){
            if(holder instanceof MyViewHolder1){
                ((MyViewHolder1)holder).playerName.setText(playerInfoBean.getName());
                ((MyViewHolder1)holder).WantWhtchCount.setText(playerInfoBean.getWantWatchConut()+"");
                ((MyViewHolder1)holder).WhtchedCount.setText(playerInfoBean.getWatchedsCount()+"");
                ((MyViewHolder1)holder).WiteCount.setText(playerInfoBean.getTopicsCount()+"");
                ImageLoaderCircleUtils.getimage(context,playerInfoBean.getImageUrl(),((MyViewHolder1)holder).playerImage);
                ImageLoaderUtils.getimage(context,playerInfoBean.getImageUrl(),((MyViewHolder1)holder).imageBackGround);
                //返回上一级的点击事件
                ((MyViewHolder1)holder).back.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        new Thread() {
                            public void run() {
                                try {
                                    Instrumentation inst = new Instrumentation();
                                    inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);//监听物理返回键
                                } catch (Exception e) {
                                    //Log.e("Exception when sendKeyDownUpSync"e.toString());
                                }
                            }
                        }.start();
                    }
                });
                ((MyViewHolder1)holder).userInfo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"用户信息",Toast.LENGTH_SHORT).show();
                    }
                });
            }

        }else {
            if(holder instanceof MyViewHolder2){
                FindComment comment = list.get(position - 1);
                ((MyViewHolder2)holder).Title.setText("《"+comment.getTitle()+"》");
                ((MyViewHolder2)holder).CommentContent.setText(comment.getContent());
            }

        }

    }

    @Override
    public int getItemCount() {
        return list.size()+1;
    }

    class MyViewHolder1 extends RecyclerView.ViewHolder{
        ImageView playerImage,back,userInfo,imageBackGround;
        TextView playerName,WantWhtchCount,WhtchedCount,WiteCount;
        public MyViewHolder1(View itemView) {
            super(itemView);
            playerImage= (ImageView) itemView.findViewById(R.id.Find_imagePlayers);
            back= (ImageView) itemView.findViewById(R.id.PlayerInfo_back);
            userInfo= (ImageView) itemView.findViewById(R.id.PlayerInfo_UserInfo);
            playerName= (TextView) itemView.findViewById(R.id.Find_namePlayer);
            WantWhtchCount= (TextView) itemView.findViewById(R.id.WantwatchCount);
            WhtchedCount= (TextView) itemView.findViewById(R.id.WatchedCount);
            WiteCount= (TextView) itemView.findViewById(R.id.WiteCount);
            imageBackGround= (ImageView) itemView.findViewById(R.id.imageBackGround);
        }
    }
    class MyViewHolder2 extends RecyclerView.ViewHolder{
        TextView Title,CommentContent;
        public MyViewHolder2(View itemView) {
            super(itemView);
            Title= (TextView) itemView.findViewById(R.id.PlayerInfo_Title);
            CommentContent= (TextView) itemView.findViewById(R.id.PlayerInfo_comment);
        }
    }
}
